import 'dart:convert';

import 'package:flustars/flustars.dart';
import 'package:xm_app/common/config/Address.dart';
import 'package:xm_app/common/http/api_response.dart';
import 'package:xm_app/common/http/http_utils.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/dao/DaoBase.dart';
import 'package:xm_app/model/FundMNFInfo.dart';

class FundDao extends DaoBase {
  //获取基金净值
  Future<ApiResponse> getFundMNFInfo(String fcodes) {
    return vipInvoke<FundMNFInfo>(
      () => HttpUtils.post(Address.getFundMNFInfo(fcodes: fcodes)),
      creator: (jsonData) => FundMNFInfo.fromJson(jsonData),
    );
  }

  Future<MNFItem> getSH000001() async {
    MNFItem item = MNFItem();
    item.fCODE = "000001";
    item.sHORTNAME = "上证指数";
    item.img = "http://img1.money.126.net/chart/hs/time/540x360/0000001.png";
    final response = await HttpUtils.post(Address.getSharesJs(code: "sh000001"));
    if (response != null) {
      String str = response.toString();
      str = str.substring(str.indexOf('"') + 1, str.lastIndexOf('"'));
      List list = str.split(",");
      double star = double.parse(list[2]);
      double end = double.parse(list[3]);
      double d = ((end - star) / star) * 100;
      item.gSZZL = "${d.toStringAsFixed(2)}";
    }
    return item;
  }
}
